package com.ifar.audiosdk.utils

import android.view.Surface
import android.view.View
import com.ifar.audiosdk.activity.MainActivity
import com.ifar.audiosdk.common.IfarEventBusTag
import com.ifar.audiosdk.common.IfarVideoConfig
import com.llvision.glass3.core.camera.client.*
import com.llvision.glass3.core.key.client.IKeyEventClient
import com.llvision.glass3.core.lcd.client.IGlassDisplay
import com.llvision.glass3.core.lcd.client.ILCDClient
import com.llvision.glass3.platform.ConnectionStatusListener
import com.llvision.glass3.platform.IGlass3Device
import com.llvision.glass3.platform.LLVisionGlass3SDK
import com.llvision.glxss.common.ui.CameraTextureView
import com.llvision.glxss.common.ui.SurfaceCallback
import kotlinx.android.synthetic.main.glass_screen.view.*
import org.simple.eventbus.EventBus

object GlassHandle {
    /**眼镜摄像头视频帧*/
    var frame: ByteArray? = null

    /**眼镜连接状态*/
    var isGlassConnect = false

    /**眼镜SDK属性*/
    private lateinit var mGlass3Device: IGlass3Device
    private lateinit var mCameraDevice: ICameraDevice
    private lateinit var mCameraClient: ICameraClient
    private lateinit var mGlassDisplay: IGlassDisplay
    private lateinit var mLcdClient: ILCDClient

    /**眼镜初始化*/
    fun initGlass(activity: MainActivity, vGlass: View) {
        LLVisionGlass3SDK.getInstance().init(activity, object : ConnectionStatusListener {
            override fun onServiceConnected(glass3Devices: List<IGlass3Device>) {}
            override fun onServiceDisconnected() {}
            override fun onDeviceConnect(device: IGlass3Device) {
                initGlassParams(device)
                openDisplay(activity, vGlass)
            }

            override fun onDeviceDisconnect(device: IGlass3Device) {
                isGlassConnect = false
            }

            override fun onError(code: Int, msg: String) {
                isGlassConnect = false
                activity.showToast(msg)
            }
        })
    }

    /**初始化眼镜SDK参数*/
    private fun initGlassParams(device: IGlass3Device) {
        isGlassConnect = true
        mGlass3Device = device

        val key = LLVisionGlass3SDK.getInstance()
            .getGlass3Client(IGlass3Device.Glass3DeviceClient.KEY) as IKeyEventClient
        key.getGlassKeyEvent(mGlass3Device).setOnGlxssFnClickListener {
            switchDisplay()
        }

        mCameraClient = LLVisionGlass3SDK.getInstance()
            .getGlass3Client(IGlass3Device.Glass3DeviceClient.CAMERA) as ICameraClient
        mLcdClient = LLVisionGlass3SDK.getInstance()
            .getGlass3Client(IGlass3Device.Glass3DeviceClient.LCD) as ILCDClient
        mGlassDisplay = mLcdClient.getGlassDisplay(mGlass3Device)

    }

    /**开启眼镜扩展画面*/
    fun openDisplay(activity: MainActivity, vGlass: View) {
        vGlass.tvTime.text = "20:22"
        mGlassDisplay.createCaptureScreen(activity, vGlass)
    }

    /**关闭眼镜扩展*/
    fun closeDisplay() {
        mGlassDisplay.stopCaptureScreen()
    }

    /**眼镜扩展画面状态切换*/
    private fun switchDisplay() {
    }

    /**开启眼镜摄像头*/
    fun openCamera() {
        EventBus.getDefault().post("", IfarEventBusTag.OPEN_GLASS_CAMERA)
        mCameraDevice = mCameraClient.openCamera(mGlass3Device, glassCameraStatusListener)
        val width = IfarVideoConfig.resolutionW
        val height = IfarVideoConfig.resolutionH
        val fps = IfarVideoConfig.FPS
        mCameraDevice.setPreviewSize(width, height, fps)
        mCameraDevice.connect()
    }

    /**关闭眼镜摄像头*/
    fun closeCamera() {
        EventBus.getDefault().post("", IfarEventBusTag.CLOSE_GLASS_CAMERA)
        mCameraDevice.disconnect()
        mCameraDevice.release()
    }

    /**眼镜摄像头连接状态监听*/
    private val glassCameraStatusListener = object : CameraStatusListener {
        override fun onCameraOpened() {}

        override fun onCameraConnected() {
            mCameraDevice.setFov(3)     //设置焦距
            mCameraDevice.setFrameCallback(frameCallBack, PixelFormat.PIXEL_FORMAT_NV21)
        }

        override fun onCameraDisconnected() {}

        override fun onCameraClosed() {}

        override fun onError(p0: Int) {}
    }

    /**眼镜摄像头返回数据监听*/
    val frameCallBack = IFrameCallback {
        frame = it
    }

    /**眼镜摄像头预览*/
    fun actionSurfaceView(cameraView: CameraTextureView) {
        cameraView.setSurfaceCallback(surfaceCallback)
    }

    private val surfaceCallback = object : SurfaceCallback {
        override fun onSurfaceCreated(surface: Surface?) {
            mCameraDevice.addSurface(surface, false)
        }

        override fun onSurfaceChanged(surface: Surface?, p1: Int, p2: Int) {}
        override fun onSurfaceDestroy(surface: Surface?) {
        }

        override fun onSurfaceUpdate(surface: Surface?) {}
    }
}